<template>
  <div class="business-order-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="24">
        <el-form :inline="true" :model="queryForm" @submit.prevent>
          <el-space wrap :size="size">
            <el-form-item class="form-item">
              <el-cascader
                v-model="queryForm.cityArea"
                :options="cityOptions"
                :props="citySelect"
                placeholder="选择地区"
                clearable
              />
            </el-form-item>
            <el-form-item class="form-item">
              <el-cascader
                v-model="queryForm.industry"
                :options="industryOptions"
                :props="industrySelect"
                placeholder="选择行业"
                clearable
              />
            </el-form-item>
            <el-form-item>
              <el-input
                v-model.trim="queryForm.searchKey"
                clearable
                placeholder="请输入客户名称"
                @input="trimkey"
              />
            </el-form-item>
            <el-form-item>
              <el-input
                v-model.trim="queryForm.pickStaff"
                clearable
                placeholder="请输签约人"
              />
            </el-form-item>
            <!-- <el-form-item>
              <el-input
                v-model.trim="queryForm.storeAccount"
                clearable
                placeholder="请输入店铺账号"
              />
            </el-form-item> -->
            <el-form-item>
              <el-input
                v-model.trim="queryForm.orderCode"
                clearable
                placeholder="请输合同编号"
              />
            </el-form-item>
            <el-form-item>
              <!-- <el-date-picker
                v-model="queryForm.orderDate"
                type="date"
                placeholder="请选择到单录入时间"
              /> -->
              <DateSelect ref="DateSelectRef" @get-date="getDate" />
            </el-form-item>
            <el-form-item>
              <el-button :icon="Search" type="primary" @click="queryData">
                查询
              </el-button>
            </el-form-item>
          </el-space>
        </el-form>
      </vab-query-form-left-panel>
    </vab-query-form>

    <el-tabs
      v-model="queryForm.tab"
      type="border-card"
      @tab-click="handleClick"
    >
      <el-tab-pane label="新到单待录入" name="new">
        <el-table
          v-loading="listLoading"
          :data="list"
          @selection-change="setSelectRows"
        >
          <!-- <el-table-column align="center" show-overflow-tooltip type="selection" /> -->
          <el-table-column
            align="center"
            label="ID"
            prop="OrderID"
            show-overflow-tooltip
            width="80"
          />
          <el-table-column
            align="center"
            label="报签时间"
            prop="SignedStatusTime"
            show-overflow-tooltip
            width="200"
          />
          <el-table-column
            align="center"
            label="客户名称"
            prop="CustomerAllName"
            show-overflow-tooltip
            width="200"
          />
          <el-table-column
            align="center"
            label="签约业务类型"
            prop="BusinessOrderName"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            label="签约人"
            prop="SaleStaffName"
            show-overflow-tooltip
          />
          <el-table-column align="center" label="返退重录" width="200">
            <template #default="{ row }">
              <el-button
                v-if="row.OrderFlowState == -1"
                type="danger"
                @click="handleEdit(row)"
              >
                返退重录
              </el-button>
            </template>
          </el-table-column>
          <el-table-column align="center" label="操作" width="200">
            <template #default="{ row }">
              <el-button type="primary" @click="handleEdit(row)">
                到单录入
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          background
          :current-page="queryForm.pageNo"
          :layout="layout"
          :page-size="queryForm.pageSize"
          :total="total"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        />
      </el-tab-pane>
      <el-tab-pane label="已录入" name="inputed">
        <el-table
          v-loading="listLoading"
          :data="list"
          @selection-change="setSelectRows"
        >
          <!-- <el-table-column align="center" show-overflow-tooltip type="selection" /> -->
          <el-table-column
            align="center"
            label="ID"
            prop="OrderID"
            show-overflow-tooltip
            width="80"
          />
          <el-table-column align="center" label="到单录入时间" width="200">
            <template #default="{ row }">
              {{ getOrderTime(row) }}
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="合同编号"
            prop="OrderCode"
            show-overflow-tooltip
            width="200"
          />
          <el-table-column
            align="center"
            label="客户名称"
            prop="CustomerAllName"
            show-overflow-tooltip
            width="200"
          />

          <el-table-column
            align="center"
            label="签约业务类型"
            prop="BusinessOrderName"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            label="签约人"
            prop="SaleStaffName"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            label="签单日期"
            prop="SignOrderDate"
            show-overflow-tooltip
            width="120"
          />
          <el-table-column align="center" label="操作" width="200">
            <template #default="{ row }">
              <el-button type="success" @click="handleEdit(row)">
                查看详情
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          background
          :current-page="queryForm.pageNo"
          :layout="layout"
          :page-size="queryForm.pageSize"
          :total="total"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        />
      </el-tab-pane>
      <el-tab-pane
        v-if="loginUser.StaffPostRoleID !== 8"
        label="下属新到单待录入"
        name="subnew"
      >
        <el-table
          v-loading="listLoading"
          :data="list"
          @selection-change="setSelectRows"
        >
          <!-- <el-table-column align="center" show-overflow-tooltip type="selection" /> -->
          <el-table-column
            align="center"
            label="ID"
            prop="OrderID"
            show-overflow-tooltip
            width="80"
          />
          <el-table-column
            align="center"
            label="报签时间"
            prop="addTime"
            show-overflow-tooltip
            width="200"
          />
          <el-table-column
            align="center"
            label="客户名称"
            prop="CustomerAllName"
            show-overflow-tooltip
            width="200"
          />
          <el-table-column
            align="center"
            label="签约业务类型"
            prop="BusinessOrderName"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            label="签约人"
            prop="SaleStaffName"
            show-overflow-tooltip
          />
          <el-table-column align="center" label="返退重录" width="200">
            <template #default="{ row }">
              <el-button
                v-if="row.OrderFlowState == -1"
                type="danger"
                @click="handleEdit(row)"
              >
                返退重录
              </el-button>
            </template>
          </el-table-column>
          <el-table-column align="center" label="操作" width="200">
            <template #default="{ row }">
              <el-button type="primary" @click="handleEdit(row)">
                到单录入
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          background
          :current-page="queryForm.pageNo"
          :layout="layout"
          :page-size="queryForm.pageSize"
          :total="total"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        />
      </el-tab-pane>
      <el-tab-pane
        v-if="loginUser.StaffPostRoleID !== 8"
        label="下属已录入"
        name="subinputed"
      >
        <el-table
          v-loading="listLoading"
          :data="list"
          @selection-change="setSelectRows"
        >
          <!-- <el-table-column align="center" show-overflow-tooltip type="selection" /> -->
          <el-table-column
            align="center"
            label="ID"
            prop="OrderID"
            show-overflow-tooltip
            width="80"
          />
          <el-table-column align="center" label="到单录入时间" width="200">
            <template #default="{ row }">
              {{ getOrderTime(row) }}
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="合同编号"
            prop="OrderCode"
            show-overflow-tooltip
            width="200"
          />
          <el-table-column
            align="center"
            label="客户名称"
            prop="CustomerAllName"
            show-overflow-tooltip
            width="200"
          />

          <el-table-column
            align="center"
            label="签约业务类型"
            prop="BusinessOrderName"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            label="签约人"
            prop="SaleStaffName"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            label="签单日期"
            prop="SignOrderDate"
            show-overflow-tooltip
            width="120"
          />
          <el-table-column align="center" label="操作" width="200">
            <template #default="{ row }">
              <el-button type="success" @click="handleEdit(row)">
                查看详情
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          background
          :current-page="queryForm.pageNo"
          :layout="layout"
          :page-size="queryForm.pageSize"
          :total="total"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        />
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
  import DateSelect from '/src/components/DateSelect.vue'
  import { CitySelect, IndustrySelect } from '@/api/common'

  import { getBusinessList, doDelete } from '@/api/businessOrder'
  import { Delete, Plus, Search } from '@element-plus/icons-vue'

  export default defineComponent({
    name: 'CreateIndex',
    components: {
      DateSelect,
    },
    setup() {
      // const route = useRoute() //
      const router = useRouter()
      const $baseConfirm = inject('$baseConfirm')
      const $baseMessage = inject('$baseMessage')

      const state = reactive({
        loginUser: {},
        editRef: null,
        customer: {},
        list: [],
        cityOptions: [],
        industryOptions: [],
        citySelect: { checkStrictly: true, multiple: true },
        industrySelect: { checkStrictly: true, multiple: true },
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        DateSelectRef: null,
        queryForm: {
          cityArea: {},
          industry: {},
          searchKey: '',
          orderCode: '',
          pickStaff: '',
          searchOrderFlow: 2,
          orderDate: '',
          dataType: 'create',
          tab: 'new',
          pageNo: 1,
          pageSize: 10,
        },
        activeName: '',
      })

      const trimkey = (val) => {
        state.queryForm.searchKey = val.replace(
          /[^\u4e00-\u9fa5a-zA-Z0-9]/g,
          ''
        )
      }

      const handleClick = (tab) => {
        console.log(tab.props.name)
        state.queryForm.tab = tab.props.name
        fetchData()
      }

      const getOrderTime = (row) => {
        if (row.OrderFlowState > 1 && row.OrderFlow) {
          const addtimeJson = row.OrderFlow.find(
            (item) => item.OrderFlowState === 2
          )
          if (addtimeJson) return addtimeJson.addTime
        } else {
          return '---'
        }
      }

      const createOrder = () => {
        router.push({
          path: 'createOrder',
          query: { customerid: state.queryForm.customerid },
        })
      }

      const setSelectRows = (val) => {
        state.selectRows = val
      }
      const handleEdit = (row) => {
        router.push({
          path: 'createOrder',
          query: { orderid: row.OrderID },
        })
      }
      const handleDelete = (row) => {
        if (row.id) {
          $baseConfirm('你确定要删除当前项吗', null, async () => {
            const { msg } = await doDelete({ ids: row.id })
            $baseMessage(msg, 'success', 'vab-hey-message-success')
            await fetchData()
          })
        } else {
          if (state.selectRows.length > 0) {
            const ids = state.selectRows.map((item) => item.id).join()
            $baseConfirm('你确定要删除选中项吗', null, async () => {
              const { msg } = await doDelete({ ids })
              $baseMessage(msg, 'success', 'vab-hey-message-success')
              await fetchData()
            })
          } else {
            $baseMessage('未选中任何行', 'error', 'vab-hey-message-error')
          }
        }
      }
      const handleSizeChange = (val) => {
        state.queryForm.pageSize = val
        fetchData()
      }
      const handleCurrentChange = (val) => {
        state.queryForm.pageNo = val
        fetchData()
      }
      const queryData = () => {
        state.queryForm.pageNo = 1
        fetchData()
      }
      const fetchData = async () => {
        state.listLoading = true
        const {
          data: { list, total },
        } = await getBusinessList(state.queryForm)
        state.list = list
        state.total = total
        state.listLoading = false
      }
      onMounted(() => {
        state.loginUser = JSON.parse(localStorage.getItem('userInfo'))
        fetchData()
        getCitySelect()
        getIndustrySelect()
      })
      const getCitySelect = async () => {
        const {
          data: { cityOptions },
        } = await CitySelect()
        state.cityOptions = cityOptions
        console.log('列表1', cityOptions)
      }
      const getIndustrySelect = async () => {
        const {
          data: { industryOptions },
        } = await IndustrySelect()
        state.industryOptions = industryOptions
        console.log('列表2', industryOptions)
      }

      const getDate = (data) => {
        state.queryForm.orderDate = data
        console.log(data, '子组件传过来的时间数组')
      }

      return {
        ...toRefs(state),
        handleClick,
        getOrderTime,
        getDate,
        getCitySelect,
        getIndustrySelect,
        createOrder,
        setSelectRows,
        handleEdit,
        handleDelete,
        handleSizeChange,
        handleCurrentChange,
        queryData,
        fetchData,
        Delete,
        Plus,
        Search,
        trimkey,
      }
    },
  })
</script>
